package Greedy;

import java.util.ArrayList;
import java.util.List;

public class _763_PartitionLabels {
    //hint:during every loop,find the current letter's next position,if the next position is bigger than the endpos,
    //change the endpos to the next position,if not(don't have next position or the position is less than the endpos),
    // if loop's positon equals to the endpos,which means the string between startpos and endpos are samllest and appears in
    //only one parts
    public List<Integer> partitionLabels(String S) {
        List<Integer> result = new ArrayList<>();
        int length = S.length();
        int startPos = 0,endPos = 0;
        for (int i = 0; i < length; i++) {
            char tempC = S.charAt(i);
            int next = S.indexOf(tempC, i + 1);
            if (next!=-1) {
                if (endPos < next) {
                    endPos = next;
                }
            }
            if (i == endPos) {
                result.add(endPos - startPos + 1);
                endPos = startPos = i + 1;
            }
        }
        return result;
    }
}
